package com.koron.order.overhaul.notified;

import com.alibaba.fastjson.JSON;
import com.koron.common.core.business.workflow.bean.po.SysWorkflow;
import com.koron.common.core.business.workflow.bean.properties.WorkflowProperties;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.order.overhaul.bean.vo.OverhaulOrderDetailVO;
import com.koron.order.overhaul.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author twcao
 * @title: OverhaulOrderToAccept
 * @projectName GDH_EAM_V2
 * @description: 大修工单-待验收
 * @date 2021/10/815:05
 */
@Slf4j
@Component
public class OverhaulOrderToAccept implements OverhaulOrderNotify<OverhaulOrderDetailVO> {

    @Autowired
    private SysWorkflowService workflowService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    @Override
    public boolean execute(SessionFactory factory, OverhaulOrderDetailVO vo) {
        log.info("大修工单-待验收，vo = {}", JSON.toJSONString(vo));
        // 启动工作流，待工作流执行完成，自动进入下一节点状态
        List<SysWorkflow> sysWorkflows = workflowService.queryByDataId(vo.getId());
        if(!CollectionUtils.isEmpty(sysWorkflows)) {
            return true;
        }
        Map<String, String> formVariables = new HashMap<>(2);
        WorkflowProperties prop = WorkflowProperties.init(Constants.OVERHAUL_PROCESS_CODE);
        SysWorkflow workflow = workFlowHandler.start(prop, vo.getId(), vo.getCode(), vo.getTitle(), formVariables);
        log.info("工作流创建成功， billId = {}", workflow.getBillId());
        return true;
    }

    @Override
    public int getOrder() {
        return 90005;
    }

    @Override
    public String status() {
        return Constants.OverhaulStatus.TO_ACCEPT;
    }
}
